//
//  DeadlineTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2022/5/16.
//

import UIKit

protocol DeadlineTableViewCellDelegate {
    func selectDeadline(deadlines: Int)
}

class DeadlineTableViewCell: UITableViewCell {
    var titleLab: UILabel!
    var collectionView: UICollectionView!
    var bgView: UIView!
    var contentLab: UILabel!
    var seletIndex = 1
    var delegate: DeadlineTableViewCellDelegate!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.initSubView()
        self.addLayout()
    }
    
    func initSubView() {
        
        self.titleLab = UILabel(frame: CGRect(x: 14, y: 20, width: kScreenWidth - 28, height: 20))
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.contentView.addSubview(self.titleLab)
        self.titleLab.text = MMLocalizedString("deadline", comment: "贷款期数")
        
        
        let layout = UICollectionViewFlowLayout()
        let width = (kScreenWidth - 36 - 5*4)/4
        layout.itemSize = CGSize(width: width, height: 40)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 0
        
        self.collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        self.collectionView.register(DeadlineCollectionViewCell.self, forCellWithReuseIdentifier: "DeadlineCollectionViewCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.contentView.addSubview(self.collectionView)
        
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = UIColor(hex: 0xFFCC32,alpha: 0.15)
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = true
        self.contentView.addSubview(self.bgView)
        
        self.contentLab = UILabel(frame: .zero)
        self.contentLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.contentLab.textColor = Theme.titleLabColor
        self.contentLab.numberOfLines = 0
        self.contentLab.text = MMLocalizedString("more_details", comment: "以上供款计算只供参考，请联络Kool Car销售顾问了解有关最终之供款安排。")
        self.bgView.addSubview(  self.contentLab)
        
        
    }
    
    
    func addLayout() {
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.height.equalTo(20)
        }
        
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom).offset(14)
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.height.equalTo(40)
        }
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(self.collectionView.snp.bottom).offset(14)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.bottom.equalTo(0)
        }
        
        self.contentLab.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(self.bgView.snp.bottom).offset(-14)
        }
        
        self.updateConstraints()
    }
        
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}


extension DeadlineTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DeadlineCollectionViewCell", for: indexPath) as! DeadlineCollectionViewCell
        
        if self.seletIndex == indexPath.row {
            cell.bgView.backgroundColor = UIColor(hex: 0xFFCC32,alpha: 0.15)
            cell.bgView.layer.borderWidth = 1
            cell.bgView.layer.borderColor = UIColor(hex: 0xFFCC32).cgColor
            cell.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 14)
        }else{
            cell.bgView.backgroundColor = UIColor.init(hex: 0xF7F8FB)
            cell.bgView.layer.borderWidth = 0
            cell.bgView.layer.borderColor = UIColor.clear.cgColor
            cell.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 14)
        }
        
        
        switch indexPath.row {
        case 0:
            cell.titleLab.text = MMLocalizedString("two_year", comment: "24个月")
        case 1:
            cell.titleLab.text = MMLocalizedString("three_years", comment: "36个月")
        case 2:
            cell.titleLab.text = MMLocalizedString("four_years", comment: "48个月")
        default:
            cell.titleLab.text = MMLocalizedString("six_years", comment: "60月")
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.seletIndex = indexPath.row
        self.collectionView.reloadData()
        var date = 36
        switch indexPath.row {//期限
        case 0:
            date = 24
        case 1:
            date = 36
        case 2:
            date = 48
        default:
            date = 60
        }
       
        self.delegate?.selectDeadline(deadlines: date)
    }
    
    
}
